package com.lf.easyapi.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lf.easyapi.dao.EprojMapper;
import com.lf.easyapi.dao.InterfaceBaseMapper;
import com.lf.easyapi.dao.InterfaceParamMapper;
import com.lf.easyapi.entry.Eproj;
import com.lf.easyapi.entry.InterfaceBase;
import com.lf.easyapi.entry.InterfaceParam;
import com.lf.easyapi.util.CommFunc;

@RequestMapping("/index")
@Controller()
public class IndexController extends BaseController{
	
	@Autowired
	InterfaceBaseMapper interfaceBaseMapper;
	@Autowired
	EprojMapper eProjMapper;
	@Autowired
	InterfaceParamMapper interfaceParamMapper;

	@RequestMapping(value="/i")
	public String index(){
		return "index";
	}
	
	
	/**
	 * 跳到公共页面
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/queryCommmArgsInfo")
	public @ResponseBody Object queryCommmArgsInfo(@RequestParam(value="pid", required=false) Long pid,
			@RequestParam(value="ieType") String ieType ,HttpServletRequest request){
		Map<String, Object> map = new HashMap<String, Object>();
		Map<Long, List<InterfaceParam>> mapGroup = new HashMap<Long, List<InterfaceParam>>();
		boolean multistep = true;// 是否多层级结构数据，0(true)是， 1 (false)不是， 默认是(true)
		try {
			//1. 查询是该项目是否有多层级结构， 对应数据表e_interface_base中的 id 为-1 的记录
			InterfaceBase interfaceBase =  interfaceBaseMapper.selectByPrimaryKey(-1L);
			if(interfaceBase!=null){
				multistep = StringUtils.equals("0", interfaceBase.getMultistep()) ;
			}
			Map<String, Object> map_a= new HashMap<String, Object>();
			map_a.put("pid", pid);
			map_a.put("ieType", ieType);
			List<InterfaceParam> list =  interfaceParamMapper.selectByProjId(map_a);
			map.put("reArgs", list);
		} catch (Exception e) {
			map.put("result","1");
			map.put("msg", e);
			e.printStackTrace();
		}
		map.put("result","0");
		map.put("multistep", multistep);
		return map;
	}
	
	
	
	/**
	 * 跳到公共页面
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/addComm")
	public String addComm(){
		return "addComm";
	}
	
	/**
	 * 获取项目列表 all 
	 * @return
	 */
	@RequestMapping(value="/initProjList")
	public @ResponseBody Object initProjList(){
		Map<String, Object> map = new HashMap<String, Object>();
		String result = "0";
		logger.info("index initProjList ...");
		List<Eproj> list = new ArrayList<Eproj>() ;
		try {
			list = eProjMapper.selectAll();
		} catch (Exception e) {
			logger.info("查询结果出现异常 ...");
			result = "1";
			e.printStackTrace();
		}
		logger.info(String.format("查询结束 ...查询返回的 数据量 ：  %s ", list.size()));
		map.put("list", list);
		map.put("result", result);
		return map;
	}
	
	
	/**
	 * 获取接口列表 
	 * @return
	 */
	@RequestMapping(value="/initProjiFaceList")
	public @ResponseBody Object initProjiFaceList(@RequestParam(value="pid", required=false) Long pid){
		Map<String, Object> map = new HashMap<String, Object>();
		String result = "0";
		logger.info("index initProjiFaceList ...");
		List<InterfaceBase> list = new ArrayList<InterfaceBase>() ;
		try {
			list = interfaceBaseMapper.selectAllByProjId(pid);
		} catch (Exception e) {
			logger.info("查询结果出现异常 ...");
			result = "1";
			e.printStackTrace();
		}
		logger.info(String.format("查询结束 ...查询返回的 数据量 ：  %s ", list.size()));
		map.put("list", list);
		map.put("result", result);
		return map;
	}
	
	@RequestMapping(value="/add")
	public @ResponseBody String add(){
		logger.info("index add ...");
		InterfaceBase record = new InterfaceBase();
		record.setCreateDate(new Date());
		record.setcUserId("test");
		record.seteSort(1);
		record.seteStatus("0");
		record.setiCode("sjyyt");
		record.setiDesc("test");
		record.setiName("zzzz");
		record.setiReqMode("GET");
		int row = interfaceBaseMapper.insert(record);
		logger.info("row ： " + row);
		logger.info("id ： " + record.getId());
		return row +"---"+ record.getId() +"";
	}
	@RequestMapping(value="/add2")
	public @ResponseBody String add2(){
		logger.info("index add ...");
		InterfaceBase record = new InterfaceBase();
		record.setCreateDate(new Date());
		record.setcUserId("test");
		record.seteSort(1);
		record.seteStatus("0");
		record.setiCode("sjyyt");
		record.setiDesc("test");
		record.setiName("zzzz");
		record.setiReqMode("GET");
		int row = interfaceBaseMapper.insertSelective(record);
		logger.info("row ： " + row);
		logger.info("id ： " + record.getId());
		return row +"---"+ record.getId() +"";
	}
	
	
	
}
